package classes;

import java.awt.Toolkit;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.swing.JTextField;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;

public class Utils {

	/**
	 * Function for printing a log to the default STDOUT
	 * @param tag	Log's TAG
	 * @param log	Log's content
	 */
	public static void logC(String tag, String log) {
		String timeStamp = new SimpleDateFormat("HH:mm:ss-dd/mm").format(Calendar.getInstance().getTime());
		System.out.println("[" + timeStamp + "] " + tag + ": " + log);
	}
	
	
	/**
	 * Class used to add a number filter to a text input field
	 * @see JTextField
	 */
	public static class NumberFilter extends DocumentFilter {
		/**
         * Number of characters allowed.
         */
        private int length = 0;

        /**
         * Restricts the number of characters can be entered by given length.
         * @param length Number of characters allowed.
         */
        public NumberFilter(int length) {
            this.length = length;
        }

        @Override
        public void insertString(FilterBypass fb, int offset, String string,
                AttributeSet attr) throws
                BadLocationException {
            if (isNumeric(string)) {
                if (this.length > 0 && fb.getDocument().getLength() + string.
                        length()
                        > this.length) {
                    return;
                }
                super.insertString(fb, offset, string, attr);
            } else {
            	Toolkit.getDefaultToolkit().beep();
            }
        }

        @Override
        public void replace(FilterBypass fb, int offset, int length, String text,
                AttributeSet attrs) throws
                BadLocationException {
            if (isNumeric(text)) {
                if (this.length > 0 && fb.getDocument().getLength() + text.
                        length()
                        > this.length) {
                    return;
                }
                super.insertString(fb, offset, text, attrs);
            } else {
            	Toolkit.getDefaultToolkit().beep();
            }
        }

        /**
         * This method tests whether given text can be represented as number.
         * This method can be enhanced further for specific needs.
         * @param text Input text.
         * @return {@code true} if given string can be converted to number; otherwise returns {@code false}.
         */
        private boolean isNumeric(String text) {
            if (text == null || text.trim().equals("")) {
                return false;
            }
            for (int iCount = 0; iCount < text.length(); iCount++) {
                if (!Character.isDigit(text.charAt(iCount))) {
                    return false;
                }
            }
            return true;
        }
	}
}
